package pluginConvertisseur;
import graph.Tableur;

import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import compatibilite.AExcelMiniModele;

/**************
 * 
 * @author Axel
 *
 */
public class ModeleConvertisseur extends AExcelMiniModele 
{
	HashMap<String,HashMap<String,HashMap<String,Float>>> listeGenerale;
	HashMap<String,HashMap<String,Float>> listeUnites;
	ArrayList<Float> collectionSource;
	ArrayList<Float> collectionResultat;
	String uniteO;
	String uniteR;
	String categorie;
	
	public ModeleConvertisseur(){
		listeGenerale = new HashMap<String,HashMap<String,HashMap<String,Float>>>();
		listeUnites = new HashMap<String,HashMap<String,Float>>();
		collectionSource = new ArrayList<Float>();
		collectionResultat = new ArrayList<Float>();
		//monnaies 
		HashMap<String, Float> euros = new HashMap<String,Float>();
		euros.put("Francs", new Float(6.55957));
		euros.put("Dollars", new Float(1.320));
		listeUnites.put("Euros", euros);
		
		HashMap<String, Float> dollars = new HashMap<String,Float>();
		dollars.put("Francs", new Float(4.96937));
		dollars.put("Euros", new Float(0.75757));
		listeUnites.put("Dollars", dollars);
		
		HashMap<String, Float> francs = new HashMap<String,Float>();
		francs.put("Dollars", new Float(0.20123));
		francs.put("Euros", new Float(0.15245));
		listeUnites.put("Francs", francs);
		
		listeGenerale.put("Monnaies",listeUnites);
		//poids
		listeUnites = new HashMap<String,HashMap<String,Float>>();
		HashMap<String, Float> kilogrammes  = new HashMap<String,Float>();
		kilogrammes.put("Livres", new Float(2.2046));
		listeUnites.put("Kilogrammes", kilogrammes);
		HashMap<String, Float> livres  = new HashMap<String,Float>();
		livres.put("Kilogrammes", new Float(0.4536));
		listeUnites.put("Livres", livres);
		listeGenerale.put("Poids",listeUnites);
		//longeurs
		listeUnites = new HashMap<String,HashMap<String,Float>>();
		HashMap<String, Float> cm  = new HashMap<String,Float>();
		cm.put("Pieds", new Float(3.2809));
		HashMap<String, Float> pieds  = new HashMap<String,Float>();
		pieds.put("Centimètres", new Float(0.3048));
		listeUnites.put("Pieds", pieds);
		listeUnites.put("Centimètres", cm);
		HashMap<String, Float> miles  = new HashMap<String,Float>();
		miles.put("Kilomètres", new Float(1.609));
		HashMap<String, Float> kilometres  = new HashMap<String,Float>();
		kilometres.put("Miles", new Float(0.6215));
		listeUnites.put("Miles", miles);
		listeUnites.put("Kilomètres", kilometres);
		listeGenerale.put("Longueurs",listeUnites);
		
	}
	
	/*private void convertirListe(String UniteSource, String UniteDestination) {
		ArrayList<Float> liste = convertTableModelToArray(tm,new Float(null));
		HashMap<String,Float> listeD = null;
		ArrayList<Float> listeRetour = null;
		Float taux = null;
		if(listeUnites.containsKey(UniteSource)){
			listeD = listeUnites.get(UniteSource);
			if(listeD.containsKey(UniteDestination)){
				taux = listeD.get(UniteDestination);
			}
		}
		if(!(taux.equals(null))){
			listeRetour = new ArrayList<Float>();
			for(Float f : liste){
				listeRetour.add(f*taux);
			}
			collectionResultat = listeRetour;
		}
		
	}*/
	public Float convertirValeur(String categorie, String origine, String destination,Float valeur) {
		//valeur pouvant etre null, aConvertir permet d'eviter nullpointerexeption
		HashMap<String,Float> listeD = null;
		Float valueRetour = null;
		Float taux = null;
		if(listeGenerale.containsKey(categorie)){
			listeUnites=listeGenerale.get(categorie);
		if(listeUnites.containsKey(origine)){
			listeD = listeUnites.get(origine);
			if(listeD.containsKey(destination)){
				taux = new Float(listeD.get(destination));
			}
		}
		try{
			if(!(taux==null && valeur.equals(null))){
				valueRetour = new Float(valeur*taux);
			}
		}
		catch(NullPointerException e){}
		}
		return valueRetour;	
	}
	public void convertirTableModel(TableModel aConvertir){
		DefaultTableModel tmr = new DefaultTableModel();
		tmr.setColumnCount(aConvertir.getColumnCount());
		tmr.setRowCount(aConvertir.getRowCount());
		int i;
		for(i=0;i<aConvertir.getRowCount();i++){
			tmr.setValueAt(convertirValeur(categorie,uniteO,uniteR,(Float)aConvertir.getValueAt(i, 0)), i, 0);
		}
		majObserver("converti",tmr);
	}
	public void ajouterTauxConversion(String uniteA, String uniteB, Float taux){
		HashMap<String,Float> couple = new HashMap<String,Float>();
		couple.put(uniteB, taux);
		listeUnites.put(uniteA, couple);
	}
	public void supprimerTauxConversion(String uniteA, String uniteB){
		if(listeUnites.containsKey(uniteA)){
			listeUnites.get(uniteA).remove(uniteB);
		}
	}
	@Override
	public void charger(Tableur tableur) 
	{
		this.tableur = tableur;
		this.jt = tableur.getTableur();
		this.tm = this.jt.getModel();
	}

	public HashMap<String, HashMap<String, Float>> getListes() {
		return listeUnites;
	}

	public void setListes(HashMap<String, HashMap<String, Float>> listes) {
		this.listeUnites = listes;
	}


	public ArrayList<Float> getCollectionSource() {
		return collectionSource;
	}

	public ArrayList<Float> getCollectionResultat() {
		return collectionResultat;
	}


	

	public String getUniteO() {
		return uniteO;
	}

	public void setUniteO(String uniteO) {
		this.uniteO = uniteO;
	}

	public String getUniteR() {
		return uniteR;
	}

	public void setUniteR(String uniteR) {
		this.uniteR = uniteR;
	}

	public HashMap<String, HashMap<String, HashMap<String, Float>>> getListeGenerale() {
		return listeGenerale;
	}

	public String getCategorie() {
		return categorie;
	}

	public void setCategorie(String categorie) {
		this.categorie = categorie;
	}
}
